//
//  JYStatusViewModel.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/4.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

class JYStatusViewModel: NSObject {
    // MARK:- 定义属性
    var status : JYStatus?
    
    //定义cell高度属性
    var cellHeight : CGFloat = 0
    
    // MARK:- 对数据处理的属性
    ///来源
    var sourceText : String?
    ///时间
    var createdAtText : String?
    ///用户认证类型图片
    var verifiedImage : UIImage?
    ///会员等级图片
    var vipImage : UIImage?
    //头像url
    var profileUrl : NSURL?
    //配图URL
    var picURLs : [NSURL] = [NSURL]()
    
    // MARK:- 自定义构造函数
    init(status : JYStatus) {
        self.status = status
        
        //处理来源
        if let source = status.source where source != "" {
            //截取字符串
            let startIndex = (source as NSString).rangeOfString(">").location + 1
            let length = (source as NSString).rangeOfString("</").location - startIndex
            sourceText = (source as NSString).substringWithRange(NSRange(location: startIndex, length: length))
        }
        
        //时间处理
        if let created_at = status.created_at {
            createdAtText = NSDate.createDateString(created_at)
        }
        
        //认证图片处理
        let verifiedType = status.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //会员等级图片处理
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        //头像URL处理
        let profileUrlString = status.user?.profile_image_url ?? ""
        profileUrl = NSURL(string: profileUrlString)
        
        //配图URL处理
        let picURLDicts = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        
        if let picURLDicts = picURLDicts {
            for picURLDict in picURLDicts {
                guard let picURLString = picURLDict["thumbnail_pic"] else {
                    continue
                }
                picURLs .append(NSURL(string: picURLString)!)
            }
        }
        
        
    }
}
